package com.ynet.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.ynet.http.ForReq;
import com.ynet.utils.PropertiesUtils;

public class LogonView extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jPanel;
	
	private JTextField passwordTextField;
	private JTextField userNameTextField;

	private JButton logonButton, regButton, queryPWButton;
	private JTextArea serverUrlTextField;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LogonView frame = new LogonView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public LogonView() {
		
		JPanel north = new JPanel();
		getContentPane().add(north, BorderLayout.NORTH);
		north.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
				
		ImageIcon ico=new ImageIcon("com/ynet/bananer.png");
		ico.setImage(ico.getImage().getScaledInstance(260,100,Image.SCALE_DEFAULT));
		
		JLabel label = new JLabel(ico);
		
		label.setFont(new Font("宋体", Font.PLAIN, 35));
		north.add(label);
		
		JPanel south = new JPanel();
		getContentPane().add(south, BorderLayout.SOUTH);
		
		logonButton = new JButton("登陆");
		logonButton.addActionListener(this);
		
		logonButton.setFont(new Font("宋体", Font.PLAIN, 12));
		south.add(logonButton);
		
		regButton = new JButton("注册");
		regButton.addActionListener(this);
		regButton.setFont(new Font("宋体", Font.PLAIN, 12));
		south.add(regButton);
		
		queryPWButton = new JButton("找回密码");
		queryPWButton.setFont(new Font("宋体", Font.PLAIN, 12));
//		south.add(queryPWButton);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel userNamePanel = new JPanel();
		panel.add(userNamePanel);
		userNamePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel userNameLable = new JLabel("用户名：");
		userNameLable.setFont(new Font("宋体", Font.PLAIN, 18));
		userNameLable.setHorizontalAlignment(SwingConstants.CENTER);
		userNamePanel.add(userNameLable);
		
		userNameTextField = new JTextField();
		userNameTextField.setColumns(15);
		userNamePanel.add(userNameTextField);
		
		JPanel passwordPanel = new JPanel();
		panel.add(passwordPanel);
		passwordPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel passwordLable = new JLabel("密  码：");
		passwordLable.setFont(new Font("宋体", Font.PLAIN, 18));
		passwordLable.setHorizontalAlignment(SwingConstants.CENTER);
		passwordPanel.add(passwordLable);
		
		passwordTextField = new JTextField();
		passwordTextField.setColumns(15);
		passwordPanel.add(passwordTextField);
		
		JPanel serverUrlPanel = new JPanel();
		panel.add(serverUrlPanel);
		serverUrlPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblurl = new JLabel("服务器URL：");
		lblurl.setHorizontalAlignment(SwingConstants.CENTER);
		lblurl.setFont(new Font("宋体", Font.PLAIN, 18));
		serverUrlPanel.add(lblurl);
		
		serverUrlTextField = new JTextArea();
		serverUrlTextField.setText(new PropertiesUtils().getPropertiesValue("wanUrl"));//		http://127.0.0.1:18080/penetrate-server
		serverUrlTextField.setRows(2);
		serverUrlTextField.setColumns(15);
		
		JScrollPane jsp = new JScrollPane(serverUrlTextField);
		
		serverUrlPanel.add(jsp);
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblnr = new JLabel("\u672C\u5DE5\u5177\u7528\u4E8E\u5C06\u516C\u7F51\u8BF7\u6C42\u62A5\u6587\u8F6C\u53D1\u5230\u5185\u7F51");
		lblnr.setHorizontalAlignment(SwingConstants.CENTER);
		lblnr.setFont(new Font("宋体", Font.PLAIN, 18));
		panel_1.add(lblnr);
		
		JScrollPane scrollPane = new JScrollPane((Component) null);
		panel_1.add(scrollPane);
		this.setTitle("\u62A5\u6587\u8F6C\u53D1-\u767B\u9646");
		this.setBounds(100, 100, 400, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(true);
	}

	public void actionPerformed(ActionEvent e) {
		//登陆
		if(e.getSource() == this.logonButton){
			Map<String, Object> obj = new ForReq().logon(this.userNameTextField.getText(), this.passwordTextField.getText(), this.serverUrlTextField.getText());
			System.out.println(obj.toString());
			if(obj.get("returnCode").equals("000000")){
				System.out.println("登陆成功");
				
				Main.start(this.userNameTextField.getText(), this.passwordTextField.getText());
				this.setVisible(false);
			}else{
				System.out.println("登陆失败");
				JOptionPane.showMessageDialog(jPanel, obj.get("returnMsg"), "错误提示",JOptionPane.WARNING_MESSAGE);  
			}
		}
		//注册
		if(e.getSource() == this.regButton){

			Register frame = new Register(this.serverUrlTextField.getText());
			frame.setVisible(true);
			
			this.setVisible(false);
				
		}
		//找回密码
		if(e.getSource() == this.queryPWButton){
			
		}
	}

}
